<?php

namespace app\admin\controller;

use think\Db;

class User extends AdminController
{
    public function index()
    {
        // 原生
//        $list = Db::query('SELECT * FROM user');
        // 预处理
//        $list = Db::query('SELECT * FROM user WHERE id=?', [5]);

        // DB
//        $list = Db::table('lover')->order(['id'=>'desc'])->select();
        // 助手函数
        $list = db('user')->field(['id', 'name', 'sex'])->select();
        dump($list);
    }

    public function insert()
    {
        // 原生
//        $result = Db::execute('INSERT INTO user (id,name,age) VALUES(NULL,"pp1", "66")');
//        $result = Db::execute('INSERT INTO user (id,name,age) VALUES(:id,:name,:age)', ['id'=>null, 'name'=>'pp2', 'age'=>77]);

        $data = [
            'name' => 'pp4',
            'age' => '44'
        ];

        // DB
//        $result = Db::table('user')->insert($data);
        // 助手函数
        $result = db('user')->insert($data);

        dump($result);
    }

    public function delete()
    {
        // 原生
//        $result = Db::execute('DELETE FROM user WHERE name="老静静"');
        // DB
//        $result = Db::table('user')->delete(39);
//        $result = Db::table('user')->where('name', '小C')->delete();

        // 助手函数
        $result = db('user')->where('name', '小A')->delete();
        dump($result);
    }

    public function update()
    {
        // 原生
//        $result = Db::execute('UPDATE user SET age="19" WHERE id="1"');
        // DB
//        $result = Db::table('user')->where('id',1)->update(['age'=>'100']);
        // 助手函数
        $result = db('user')->where('id',1)->update(['age'=>'18']);

        dump($result);
    }

}
